//
//  KPTitileAndSubView.swift
//  KPBaseProject
//
//  Created by Kipp Dev on 2018/4/11.
//  Copyright © 2018年 Kipp. All rights reserved.
//

import TangramKit
import Cosmos


/// 左边标题右边子标题
class KPTitileAndSubTitle: TGRelativeLayout {

    /// 左边标题文字
    var leftText: String? {
        didSet {
            leftLabel.text = leftText
        }
    }

    /// 左边标题文字
    var rightText: String? {
        didSet {
            rightLabel.text = rightText
        }
    }

    /// 左边标题
    var leftLabel: UILabel!
    /// 右边标题
    var rightLabel: UILabel!

    override init(frame: CGRect) {
        super.init(frame: frame)
        prepareView()
    }

    convenience init() {
        self.init(frame: .zero)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // MARK: - 创建视图
    fileprivate func prepareView() {
        tg_size(width: .fill, height: 50)
        tg_leftPadding = 15.adaption
        tg_rightPadding = 15.adaption
        backgroundColor = .white

        leftLabel = UILabel(text: nil, font: 16.mediumFont, textColor: UIColor.init(hex: "292929"))
        leftLabel.tg_centerY.equal(self)
        leftLabel.tg_size(width: 30%, height: .wrap)
        leftLabel.tg_left.equal(self)
        addSubview(leftLabel)

        rightLabel = UILabel(text: nil, font: 16.font, textColor: UIColor.init(hex: "292929"))
        rightLabel.tg_centerY.equal(self)
        rightLabel.tg_size(width: .fill, height: .wrap)
        rightLabel.tg_left.equal(leftLabel.tg_right).offset(10)
        addSubview(rightLabel)
    }

}

/// 左边标题右边星级
class KPTitileAndSubCosmos: TGRelativeLayout {

    /// 左边标题
    var leftText: String? {
        didSet {
            leftLabel.text = leftText
        }
    }

    var cosmosRating: Double? {
        didSet {
            cosmosView.rating = cosmosRating.or(0)
        }
    }

    /// 左边标题
    var leftLabel: UILabel!
    /// 星级视图
    var cosmosView: CosmosView!

    override init(frame: CGRect) {
        super.init(frame: frame)
        prepareView()
    }

    convenience init() {
        self.init(frame: .zero)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // MARK: - 创建视图
    fileprivate func prepareView() {
        tg_size(width: .fill, height: 50)
        tg_leftPadding = 15.adaption
        tg_rightPadding = 15.adaption
        backgroundColor = .white

        leftLabel = UILabel(text: nil, font: 16.mediumFont, textColor: UIColor.init(hex: "292929"))
        leftLabel.tg_centerY.equal(self)
        leftLabel.tg_size(width: 30%, height: .wrap)
        leftLabel.tg_left.equal(self)
        addSubview(leftLabel)

        cosmosView = CosmosView().then {
            $0.settings.updateOnTouch = false
            $0.settings.fillMode = .half
            $0.settings.starSize = 12
            $0.settings.starMargin = 3
            $0.settings.filledColor = UIColor.init(hex: "#FFCC66")
            $0.settings.filledImage = #imageLiteral(resourceName: "五角星全选")
            $0.settings.emptyImage = #imageLiteral(resourceName: "五角星灰色")
            $0.tg_size(width: .wrap, height: .wrap)
            $0.tg_left.equal(leftLabel.tg_right).offset(10)
            $0.tg_centerY.equal(self)
            addSubview($0)
        }


    }

}
